<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="表名称" prop="tableName">
        <el-input
          v-model="queryParams.tableName"
          placeholder="请输入表名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表别名" prop="tableAlias">
        <el-input
          v-model="queryParams.tableAlias"
          placeholder="请输入表别名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="表类型" prop="tableType">
        <el-select v-model="queryParams.tableType" placeholder="请选择表类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="表状态" prop="tableStatue">
        <el-select v-model="queryParams.tableStatue" placeholder="请选择表状态" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>-->
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:table:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:table:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:table:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:table:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange" :stripe="true">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" width="150" align="center" prop="index" />
      <el-table-column label="表名称" align="left" prop="tableName" show-overflow-tooltip />
      <el-table-column label="表别名" align="left" prop="tableAlias" show-overflow-tooltip />
      <el-table-column label="表类型" align="center" show-overflow-tooltip >
        <template slot-scope="scope">
          {{scope.row.tableType | formatTableType}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:table:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:table:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改档案设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="分类编号" prop="sortNo">
              <el-input v-model="form.sortNo" placeholder="请输入分类编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="表名称" prop="tableName">
              <el-input v-model="form.tableName" placeholder="请输入表名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="表别名" prop="tableAlias">
              <el-input v-model="form.tableAlias" placeholder="请输入表别名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="顺序号" prop="orderNo">
              <el-input v-model="form.orderNo" placeholder="请输入顺序号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="表类型">
              <el-radio-group v-model="form.tableType">
                <el-radio label="0">目录表</el-radio>
                <el-radio label="1">元数据表</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="表状态">
              <el-radio-group v-model="form.tableStatue">
                <el-radio label="0">临时表</el-radio>
                <el-radio label="1">正式表</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTable, getTable, delTable, addTable, updateTable, exportTable } from "@/api/system/table";

export default {
  name: "Table",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      tNames:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 档案设置表格数据
      tableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tableName: null,
        tableAlias: null,
        tableType: null,
        tableStatue: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sortNo: [
          { required: true, message: "分类编号不能为空", trigger: "blur" }
        ],
        tableName: [
          { required: true, message: "表名称不能为空", trigger: "blur" }
        ],
        tableAlias: [
          { required: true, message: "表别名不能为空", trigger: "blur" }
        ],
        tableType: [
          { required: true, message: "表类型不能为空", trigger: "blur" }
        ],
        tableStatue: [
          { required: true, message: "表状态不能为空", trigger: "blur" }
        ],
        orderNo: [
          { required: true, message: "顺序号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询档案设置列表 */
    getList() {
      this.loading = true;
      listTable(this.queryParams).then(response => {
        this.tableList = response.rows;
        this.tableList.forEach((item,i) => {
          item.index = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + i + 1
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recNo: null,
        sortNo: null,
        tableName: null,
        tableAlias: null,
        tableType: "0",
        tableStatue: "0",
        delFlag: "0",
        orderNo: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recNo)
      this.tNames = selection.map(item => item.tableName)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加档案设置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recNo = row.recNo || this.ids
      getTable(recNo).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改档案设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.recNo != null) {
            updateTable(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTable(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const tableNames = row.tableName || this.tNames
      const recNos = row.recNo || this.ids;
      this.$confirm('是否确认删除档案设置表名称为"' + tableNames + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTable(recNos);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有档案设置数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTable(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  },
  filters:{
    formatTableType(val){
      switch (val){
        case "0":
          return "目录表"
          break
        case "1":
          return "元数据表"
          break
      }
    }
  }
};
</script>
